package com.itcast.test;

import com.itcast.catchfiveeight.CatchLinks;
import com.itcast.catchfiveeight.Contents;
import com.itcast.util.ExcelUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
public class MergeFile {

    public static List<String> list = new ArrayList<String>();
    public static void main(String[] agrs)throws Exception{
        //首先递归读取文件
        //获取到所有文件名称以及文件的路径
        String filePath = "e:\\filter";
        String ffp = "e:/zuizhong";
        String fileName = "liyuanzhuashuju.csv";
        File fileses = new File(ffp,fileName);
       // getFiles(filePath);
        int i = 0;
        test(i,filePath,fileses);
    }


    public static void test (int i,String path,File fileses) throws IOException {

        File f = new File(path);

        File[] fs = f.listFiles();

        if (fs == null) {
            return;
        }
        for (File file : fs) {
            if (file.isFile()) {
                String filePath = file.getPath();
                if(file.getPath().endsWith("xls")){
                    List<List<Object>> lists = ExcelUtils.readExcelAll(file.getPath(),1);
                    for (List<Object> lis : lists){
                        //处理每一行
                        int j =0;
                        for(Object li : lis){

                            if(j<6){
                                CatchLinks.writeElement(fileses,li.toString());
                                j++;
                            }else if(j<3){
                               continue;
                            }else{
                                break;
                            }

                        }
                        try {
                            FileUtils.write(fileses, "\r\n", "GBK", true);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
               // ExcelUtils.readExcelAllNew(file.getPath());
//                String strContent = FileUtils.readFileToString(file,"utf-8");
//                for(List<Object> lis : lists){
//                    if(strContent.contains(lis.get(1).toString())){
//                        i++;
//                        //写入编号
//                        com.itcast.bihuan.CatchLinks.writeElement(fileses,lis.get(1).toString());
//                        List<String> fileContentList = FileUtils.readLines(file);
//                        int m = 0;
//                        for(String str : fileContentList) {
//                            if(m>0 && m <3) {
//                                com.itcast.bihuan.CatchLinks.writeElement(fileses, str);
//                            }
//                            m++;
//                            if(m == 3){
//                                break;
//                            }
//                        }
//                        try {
//                            FileUtils.write(fileses, "\r\n", "GBK", true);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                        System.out.println(i);
//                    }
//                }
            } else {
                test(i,file.getPath(),fileses);
            }
        }
    }
    /*
    * 得到路径下所有文件
    */
    static void getFiles( String filePath )throws Exception
    {
        //
        File root = new File( filePath );

        File[] files = root.listFiles();
        String ffp = "e:\\zuizhong";
        String fileName = "end.csv";
        File fileses = new File(ffp,fileName);
        int j = 0;
        CatchLinks.writeTitle(fileses);
        for ( File file : files ) {
            //读取文件
            // 读取文件内容 (输入流)
            List<String> list = FileUtils.readLines(file,"GB2312");
            for(int i=1;i<list.size();i++){
                FileUtils.write(fileses,list.get(i), "GBK", true);
                FileUtils.write(fileses, "\r\n", "GBK", true);
            }
            j++;
            System.out.println(j);
        }
    }
}

